package JAVA300.第九章容器;

import java.awt.*;
import java.util.Set;
import java.util.TreeSet;

/**
 * 测试treeSet的使用
 */

public class TestTreeSet {
    public static void main(String[] args) {
        Set<Integer> set = new TreeSet<>();

        set.add(100);
        set.add(200);
        set.add(300);

        //按照元素递增的方式排序

        for(Integer m:set){
            System.out.println(m);
        }
//自定义对比内容
        Set<Student> set1 = new TreeSet<>();
        set1.add(new Student(1,99));
        set1.add(new Student(2,67));
        set1.add(new Student(3,100));

        for(Student m:set1){
            System.out.println(m);
        }

    }
}

class Student implements Comparable<Student>{
    int id;
    int grade;

    public Student(int id, int grade) {
        this.id = id;
        this.grade = grade;
    }

    @Override
    public int compareTo(Student o) {
        if (this.grade < o.grade){
            return -1;
        }else if (this.grade > o.grade){
            return 1;
        }else {
            return 0;
        }
    }

    @Override
    public String toString() {
        return "Student{" +
                "id=" + id +
                ", grade=" + grade +
                '}';
    }
}
